/**
 * Created by freethy on 2017/10/27.
 */


define(['jquery','uposs'],function ($,uposs){

    var up = function (filter,callback){

        var accessid = "LTAIvb7VvhIkKVl6";
        var accesskey = "09avuirUWct9OlhVMPRwEjYUYpTKxW";
        var host = "http://beedong.oss-cn-shanghai.aliyuncs.com";
        var key = "bee";

        function guid() {
            function S4() {
                return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
            }
            return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
        }


        $(filter).on("click",function(){

            //创建页面元素
            var fileObj = document.createElement("input");
            fileObj.setAttribute("type", "file");
            fileObj.setAttribute("accept", ".jpg,.jpeg,.gif,.png,.bmp");
            fileObj.setAttribute("style", "display:none");
            $(fileObj).change(function () {

                var index = layer.load(0, { shade:0.2 });
                var file = fileObj.files[0];
                var name = guid();
                uposs.up({
                    accessid:    accessid,
                    accesskey:   accesskey,
                    host:        host,
                    file:   file,
                    key: key + "/" + name + ".png",
                    success:function(e,option){
                        var url = option.host +"/"+ option.key;
                        callback(url);
                    },
                    progress:function(e){

                    },
                    complete:function(){
                        layer.close(index);
                    }
                });
            });
            fileObj.click();
        });
    };
    return {
        up: up
    };
});
